/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormlaporanDataMobil.java
 *
 * Created on 08 Nov 11, 0:18:52
 */
package rentalmobil.gui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import rentalmobil.entity.Laporan_mobil;
import rentalmobil.entity.dataMobil;

/**
 *
 * @author User
 */
public class FormlaporanDataMobil extends javax.swing.JFrame {

    Laporan_mobil lap = new Laporan_mobil();
    dataMobil dataM = new dataMobil();
    List<dataMobil> datamobil = new ArrayList<dataMobil>();
    List<Laporan_mobil> latih = new ArrayList<Laporan_mobil>();

    /** Creates new form FormlaporanDataMobil */
    public FormlaporanDataMobil() throws SQLException {
        initComponents();
        Laporan_mobil dta = new Laporan_mobil();
        this.loadNoPlat();
        this.isiTabelLaporan();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = this.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);

    }

    public void loadNoPlat() throws SQLException {
        comboNoPlatMobil.removeAllItems();
        datamobil = dataM.viewDataNoMobil();
        for (dataMobil d : datamobil) {
            comboNoPlatMobil.addItem(d.getNo_plat_mobil());
        }
    }

    void hapusTextField() {

        txtKondisiMobil.setText(null);
        txtTglMulaiServis.setText(null);
        txtTglSelesaiServis.setText(null);
        txtBiayaServis.setText(null);
        txtKeteranganField.setText(null);

    }

    void isiTabelLaporan() throws SQLException {
        latih = lap.viewTabelLaporanData();
        Object data[][] = new Object[latih.size()][6];
        int x = 0;
        for (Laporan_mobil d : latih) {
            data[x][0] = d.getNo_laporan();
            data[x][1] = d.getNo_plat_mobil();
            data[x][2] = d.getTgl_servis();
            data[x][3] = d.getTgl_selesai();
            data[x][4] = d.getBiaya_servis();
            data[x][5] = d.getKeterangan();
            x++;
        }
        String judul[] = {"No Laporan", "No Mobil", "Tanggal Servis", "Tanggal Selesai", "Biaya", "Keterangan"};
        LaporanTable.setModel(new DefaultTableModel(data, judul));
        jScrollPane1.setViewportView(LaporanTable);
//        Laporan_mobil dta = new Laporan_mobil();
//
//        ResultSet dt = dta.LaporanMobilliat();
//        DefaultTableModel model = new DefaultTableModel();
//        String[] kolom = {"no_plat", "tgl_servis", "tgl_selesai", "biayaservis", "keterangan"};
//        model.setColumnIdentifiers(kolom);
//        while (dt.next()) {
//            String[] temp = new String[5];
//            temp[0] = String.valueOf(dt.getObject(1));
//            temp[1] = String.valueOf(dt.getObject(2));
//            temp[2] = String.valueOf(dt.getObject(3));
//            temp[3] = String.valueOf(dt.getObject(4));
//            temp[4] = String.valueOf(dt.getObject(5));
//            model.addRow(temp);
//        }
//        LaporanTable.setModel(model);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        btn_Cek = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        txtKondisiMobil = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtTglMulaiServis = new javax.swing.JTextField();
        txtTglSelesaiServis = new javax.swing.JTextField();
        txtBiayaServis = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtKeteranganField = new javax.swing.JTextField();
        comboNoPlatMobil = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        btnSimpan = new javax.swing.JButton();
        resetbutton = new javax.swing.JButton();
        btnKeluar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        LaporanTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setText("Laporan Data Mobil");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(149, 149, 149)
                .addComponent(jLabel1)
                .addContainerGap(148, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel2.setText("id Mobil");

        btn_Cek.setText("Cek");
        btn_Cek.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_CekActionPerformed(evt);
            }
        });

        jLabel3.setText("Kondisi Mobil");

        jLabel4.setText("tgl mulai servis");

        jLabel5.setText("tgl selesai servis");

        jLabel6.setText("biaya servis");

        jLabel7.setText("Keterangan");

        comboNoPlatMobil.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel7))
                .addGap(28, 28, 28)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(comboNoPlatMobil, 0, 98, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btn_Cek))
                    .addComponent(txtKeteranganField, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                    .addComponent(txtKondisiMobil, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                    .addComponent(txtTglMulaiServis, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                    .addComponent(txtTglSelesaiServis, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                    .addComponent(txtBiayaServis, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE))
                .addGap(175, 175, 175))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(comboNoPlatMobil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_Cek))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtKondisiMobil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTglMulaiServis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTglSelesaiServis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtBiayaServis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtKeteranganField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        btnSimpan.setText("Simpan");
        btnSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimpanActionPerformed(evt);
            }
        });

        resetbutton.setText("reset");
        resetbutton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetbuttonActionPerformed(evt);
            }
        });

        btnKeluar.setText("Keluar");
        btnKeluar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnKeluarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(56, 56, 56)
                .addComponent(btnSimpan)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(resetbutton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnKeluar)
                .addContainerGap(195, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSimpan)
                    .addComponent(resetbutton)
                    .addComponent(btnKeluar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        LaporanTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "no plat mobil", "kondisi mobil", "tgl mulai servis", "tgl selesai servis", "biaya servis"
            }
        ));
        jScrollPane1.setViewportView(LaporanTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_CekActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_CekActionPerformed
        dataMobil tes = new dataMobil();
        try {
            tes.LaporanMobilView(comboNoPlatMobil.getSelectedItem().toString());
            txtKondisiMobil.setText(tes.getKondisi_Mobil());
        } catch (SQLException ex) {
            Logger.getLogger(FormlaporanDataMobil.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btn_CekActionPerformed

    private void btnSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSimpanActionPerformed
        Laporan_mobil km = new Laporan_mobil();
        try {
            if (btnSimpan.getText().equals("Simpan")) {
                km.setNo_plat_mobil(comboNoPlatMobil.getSelectedItem().toString());
                km.setTgl_servis(txtTglMulaiServis.getText());
                km.setTgl_selesai(txtTglSelesaiServis.getText());
                km.setBiaya_servis(txtBiayaServis.getText());
                km.setKeterangan(txtKeteranganField.getText());
                km.insertLaporanMobil();
                JOptionPane.showMessageDialog(this, "Data Sudah Tersimpan");
                this.hapusTextField();
                this.isiTabelLaporan();
            } else if (btnSimpan.getText().equals("Ubah")) {
                km.setId_mobil(Integer.valueOf(comboNoPlatMobil.getSelectedItem().toString()));
                km.setTgl_servis(txtTglMulaiServis.getText());
                km.setTgl_selesai(txtTglSelesaiServis.getText());
                km.setBiaya_servis(txtBiayaServis.getText());
                km.setKeterangan(txtKeteranganField.getText());
                km.updateDatalaporanMobil();
                btnSimpan.setText("Simpan");
                JOptionPane.showMessageDialog(this, "Data Sudah di Update");
                this.hapusTextField();
                this.isiTabelLaporan();
            }
        } catch (SQLException ex) {
            Logger.getLogger(FormlaporanDataMobil.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnSimpanActionPerformed

    private void btnKeluarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnKeluarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnKeluarActionPerformed

    private void resetbuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetbuttonActionPerformed
        this.hapusTextField();
    }//GEN-LAST:event_resetbuttonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new FormlaporanDataMobil().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(FormlaporanDataMobil.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable LaporanTable;
    private javax.swing.JButton btnKeluar;
    private javax.swing.JButton btnSimpan;
    private javax.swing.JButton btn_Cek;
    private javax.swing.JComboBox comboNoPlatMobil;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton resetbutton;
    private javax.swing.JTextField txtBiayaServis;
    private javax.swing.JTextField txtKeteranganField;
    private javax.swing.JTextField txtKondisiMobil;
    private javax.swing.JTextField txtTglMulaiServis;
    private javax.swing.JTextField txtTglSelesaiServis;
    // End of variables declaration//GEN-END:variables
}
